package com.bindapp.model;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.constraint.ConstraintSet;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bindapp.R;
import com.bindapp.activity.ForgetPassActivity;
import com.bindapp.activity.LoginActivity;
import com.bindapp.activity.RegActivity;
import com.bindapp.activity.TabActivity;
import com.bindapp.bean.LoginHttpBean;
import com.bindapp.databinding.ActivityLoginBinding;
import com.bindapp.http.MNetHttp;
import com.bindapp.http.PunFuncRequest;
import com.bindapp.listener.HttpListener;
import com.bindapp.util.AppManager;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by tom055 on 2018/6/23.
 */

public class LoginModel extends BaseModel {
    private ActivityLoginBinding binding;
    private ConstraintSet set1, set2;
    private View view;
    private Type selectType;
    private MaterialDialog.Builder progressDialog;



    enum Type {
        ONE, TWO;
    }

    public LoginModel(Context context) {
        super(context);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_login, null, false);
        binding.setClick(new ViewClick());
        selectType = Type.ONE;
        progressDialog=new MaterialDialog.Builder(context).content("正在加载中...").progress(true, 0).canceledOnTouchOutside(false);
        //initview();
        PunFuncRequest.requestEzToken(context, new HttpListener() {
            @Override
            public void success(Object obj) {
                //requestData();
            }

            @Override
            public void fail(String msg) {
                //Static.showToast(context,"ezToken获取失败");

            }
        });
        binding.etMobile.setText(PreferencesUtil.getInstance(context).getPhonenum());
    }

    /*private void initview() {
        float w = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70,context.getResources().getDisplayMetrics());
        set1 = new ConstraintSet();
        view = new View(context);
        view.setLayoutParams(new ConstraintLayout.LayoutParams((int)w, 2));
        binding.constraintLayout.addView(view);
        view.setBackgroundColor(ContextCompat.getColor(context, R.color.cpb_blue));
        set1.clone(binding.constraintLayout);
        set1.constrainHeight(view.getId(), 2);
        set1.connect(view.getId(), ConstraintSet.LEFT, binding.tab1.getId(), ConstraintSet.LEFT, 0);
        set1.connect(view.getId(), ConstraintSet.RIGHT, binding.tab2.getId(), ConstraintSet.LEFT, 0);
        set1.connect(view.getId(), ConstraintSet.BOTTOM, binding.constraintLayout.getId(), ConstraintSet.BOTTOM, 2);
        //constraintSet.applyTo(binding.constraintLayout);

        set2 = new ConstraintSet();
        view.setLayoutParams(new ConstraintLayout.LayoutParams((int)w, 2));
        //binding.constraintLayout.addView(view);
        view.setBackgroundColor(ContextCompat.getColor(context, R.color.cpb_blue));
        set2.clone(binding.constraintLayout);
        set2.constrainHeight(view.getId(), 2);
        set2.connect(view.getId(), ConstraintSet.LEFT, binding.tab2.getId(), ConstraintSet.LEFT, 0);
        set2.connect(view.getId(), ConstraintSet.RIGHT, binding.tab2.getId(), ConstraintSet.RIGHT, 0);
        set2.connect(view.getId(), ConstraintSet.BOTTOM, binding.constraintLayout.getId(), ConstraintSet.BOTTOM, 2);
        set1.applyTo(binding.constraintLayout);
    }


    private void animationTab(Type t) {
        TransitionManager.beginDelayedTransition(binding.constraintLayout);
        if (t == Type.ONE) {
            set1.applyTo(binding.constraintLayout);
        } else {
            set2.applyTo(binding.constraintLayout);
        }


    }*/


    public class ViewClick {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.registbtn:
                    Intent intent=new Intent(context, RegActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.forgetPass:
                    Intent intentforget=new Intent(context, ForgetPassActivity.class);
                    context.startActivity(intentforget);
                    break;
                case R.id.submit:
                    if(TextUtils.isEmpty(binding.etMobile.getText())){
                        Static.showToast(context,"用户名不能为空");
                    }else if(TextUtils.isEmpty(binding.password.getText())){
                        Static.showToast(context,"密码不能为空");
                    }else{
                        showPassDialog("正在登陆...");
                        Map<String,String> params=new HashMap<String,String>();
                        params.put("PhoneNumber",binding.etMobile.getText().toString());
                        params.put("Password",binding.password.getText().toString());

                        MNetHttp.getInstance(context).rxPostRequest(Static.LoginUrl, params, LoginHttpBean.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dissPassDialog();
                                if(((LoginHttpBean)obj).isBl()&&((LoginHttpBean)obj).getIsIsSuccess()){
                                    PreferencesUtil.getInstance(context).setPassword(binding.password.getText().toString());
                                    PreferencesUtil.getInstance(context).setPhonenum(binding.etMobile.getText().toString());
                                    Static.showToast(context,"登陆成功");
                                    PreferencesUtil.getInstance(context).setCookie(((LoginHttpBean)obj).getTickets());
                                    Static.Cookie = ((LoginHttpBean)obj).getTickets();
                                    turnToVc(TabActivity.class,new HashMap<String,Object>());
                                    ((Activity)context).finish();
                                    AppManager.finishActivity(LoginActivity.class);
                                }else{
                                    Static.showToast(context,"账号密码错误");
                                }
                            }

                            @Override
                            public void fail(String msg) {
                                dissPassDialog();
                                Static.showToast(context,msg);
                            }
                        });
                        /*Intent intentab=new Intent(context, TabActivity.class);
                        context.startActivity(intentab);
                        ((Activity)context).finish();*/
                        /*final MaterialDialog dialog=progressDialog.show();
                        Map<String,Object> params=new HashMap<String,Object>();
                        params.put("op","login");
                        params.put("password","1");
                        params.put("userId","test");
                        QueueRequest.getInstance().addListRequest(params,LoginBean.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dialog.dismiss();
                                if(((LoginBean)obj).getSuccess()){
                                    Static.myLog("成功LoginBean->"+((LoginBean)obj).getMessage());
                                    Intent intent=new Intent(context, TabActivity.class);
                                    context.startActivity(intent);
                                    ((Activity)context).finish();
                                }else{
                                    Static.myLog("失败");
                                    Static.showToast(context,"登陆失败请重试");

                                }
                            }

                            @Override
                            public void fail(String msg) {
                                Static.myLog(msg);
                                dialog.dismiss();
                            }
                        });*/
                    }


                    break;
            }

        }
    }

    @Override
    public ActivityLoginBinding getBinding() {
        return binding;
    }
}
